package com.designer.shapes
{
	
	public class Polygon extends GeometricShape
	{
		private var _nSides:uint = 5;
		private var _nLength:Number = 10;
		
		public function Polygon(lineColor:Number=0, lineThickness:Number=0, lineAlpha:Number=1, fillColor:Number=0, fillAlpha:Number=1, nSides:uint=5, xStart:Number=0, yStart:Number=0, xFinal:Number=0, yFinal:Number=0) {
			super("polygon", lineColor, lineThickness, lineAlpha, fillColor, fillAlpha, xStart, yStart, xFinal, yFinal);
			_nSides = nSides;
			_nLength = Math.sqrt((xFinal - xStart)*(xFinal - xStart) + (yFinal - yStart)*(yFinal - yStart));
			draw();
		}
		
		public override function draw():void
		{
			
			this.graphics.clear();
			
			this.graphics.lineStyle(1.0, lineColor, 1.0);
			this.graphics.beginFill(fillColor, 1.0);
			
			//this.graphics.drawRect(xStart, yStart, wid, hei); 
				
			var nX:Number = xStart;
			var nY:Number = yStart;
			
			var nRotation:Number = 0;	
			
			_nLength = Math.sqrt((xFinal - xStart)*(xFinal - xStart) + (yFinal - yStart)*(yFinal - yStart));
			nRotation = Math.asin((yFinal-yStart)/_nLength);

				
			//nRotation = nRotation * Math.PI / 180;
			// The angle formed between the segments from the polygon's center as shown in 
			// Figure 4-5. Since the total angle in the center is 360 degrees (2p radians),
			// each segment's angle is 2p divided by the number of sides.
			var nAngle:Number = (2 * Math.PI) / _nSides;
					  // Calculate the length of the radius that circumscribes the polygon (which is
			// also the distance from the center to any of the vertices).
			var nRadius:Number = (_nLength/2)/Math.sin(nAngle/2);
					  // The starting point of the polygon is calculated using trigonometry where 
			// radius is the hypotenuse and nRotation is the angle.
			var nPx:Number = (Math.cos(nRotation) * nRadius) + nX;
			var nPy:Number = (Math.sin(nRotation) * nRadius) + nY;
					  // Move to the starting point without yet drawing a line.
			this.graphics.moveTo(nPx, nPy);
					  // Draw each side. Calculate the vertex coordinates using the same trigonometric
			// ratios used to calculate px and py earlier.
			for (var i:Number = 1; i <= _nSides; i++) {
				nPx = (Math.cos((nAngle * i) + nRotation) * nRadius) + nX;
				nPy = (Math.sin((nAngle * i) + nRotation) * nRadius) + nY;
				this.graphics.lineTo(nPx, nPy);
			}
			this.graphics.endFill();
		}		
	}
}